package com.zhu.mapper.typeHandler;

import com.zhu.util.ObjectUtil;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by DELL on 2015/12/31.
 */
@MappedTypes(Session.class)
public class SessionTypeHandler implements TypeHandler<Session> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Session session, JdbcType jdbcType) throws SQLException {
        byte[] by = ObjectUtil.objectToByteArray(session);
        preparedStatement.setBlob(i, new ByteArrayInputStream(by));
    }

    @Override
    public Session getResult(ResultSet resultSet, String s) throws SQLException {
        return ObjectUtil.byteArrayToObject(resultSet.getBytes(s));
    }

    @Override
    public Session getResult(ResultSet resultSet, int i) throws SQLException {
        return ObjectUtil.byteArrayToObject(resultSet.getBytes(1));
    }

    @Override
    public Session getResult(CallableStatement callableStatement, int i) throws SQLException {
        return ObjectUtil.byteArrayToObject(callableStatement.getBytes(i));
    }
}
